"""全线程启动程序"""

from src.core import log
from src.core import Enums
from threading import Thread
from src.api.router import join
from src.core.variables.constant import *
from src.apps.hosting import self_start as hosting_self_start


def master_api():
    try:
        app = join()
        run_port = CONF.LOCAL_PORT - 1
        match CONF.LOG_LEVEL:
            case Enums.LogLevel.DEBUG:
                app.run(
                    host=CONF.LOCAL_IP,
                    port=run_port,
                    debug=False,
                    use_reloader=False,
                )
                log.INFO(
                    f"Flask API 启动成功(开发模式): http://{CONF.LOCAL_IP}:{run_port}"
                )
            case Enums.LogLevel.INFO:
                app.run(
                    host=CONF.LOCAL_IP,
                    port=run_port,
                    debug=False,
                    use_reloader=False,
                )
                log.INFO(
                    f"Flask API 启动成功(生产模式): http://{CONF.LOCAL_IP}:{run_port}"
                )
    except Exception as e:
        log.ERROR(f"启动失败: {repr(e)}")
        exit()


def main():
    hosting_self_start()
    t_api = Thread(target=master_api, name="all.api")
    t_api.start()
